Return to start page

Core/Debug/Struct Benchmark.j

Code

		
1			library AStructCoreDebugBenchmark requires AStructCoreGeneralAsl, AStructCoreGeneralVector
2
3 /**
4 * This struct can be used for time measurement of important code parts.
5 * If you're using the japi it uses specific watch natives for exact time measures.
6 * Otherwise it uses the default Warcraft @type timer.
7 */
8 struct ABenchmark
9 //static members
10 private static AIntegerVector m_benchmarks
11 private static AUnitVector m_units
12 private static AItemVector m_items
13 private static ADestructableVector m_destructables
14 private static AEffectVector m_effects
15 //dynamic members
16 private string m_name
17 //members
18 private boolean m_isRunning
19 private real m_time
20 static if (A_JAPI) then
21 private integer m_stopWatch
22 else
23 private timer m_timer
24 endif
25 private integer m_index
26
27 //dynamic members
28
29 public method setName takes string name returns nothing
30 set this.m_name = name
31 endmethod
32
33 public method name takes nothing returns string
34 return this.m_name
35 endmethod
36
37 //members
38
39 public method isRunning takes nothing returns boolean
40 return this.m_isRunning
41 endmethod
42
43 public method time takes nothing returns real
44 return this.m_time
45 endmethod
46
47 //methods
48
49 /// @todo When timer ends it should be started again and elapsed time should be added to member variable.
50 public method start takes nothing returns nothing
51 set this.m_isRunning = true
52 set this.m_time = 0.0
53 static if (A_JAPI) then
54 set this.m_stopWatch = StopWatchCreate()
55 else
56 call TimerStart(this.m_timer, 99999.0, false, null)
57 endif
58 endmethod
59
60 public method stop takes nothing returns nothing
61 set this.m_isRunning = false
62 static if (A_JAPI) then
63 set this.m_time = 1000 * StopWatchMark(this.m_stopWatch)
64 call StopWatchDestroy(this.m_stopWatch)
65 set this.m_stopWatch = -1
66 else
67 set this.m_time = TimerGetElapsed(this.m_timer)
68 call PauseTimer(this.m_timer)
69 endif
70 endmethod
71
72 public method show takes nothing returns nothing
73 debug call Print(this.m_name + ": " + R2S(this.m_time))
74 endmethod
75
76 public static method create takes string name returns thistype
77 local thistype this = thistype.allocate()
78 //dynamic members
79 set this.m_name = name
80 //members
81 set this.m_isRunning = false
82 set this.m_time = 0
83 static if (A_JAPI) then
84 set this.m_stopWatch = -1 //0?
85 else
86 set this.m_timer = CreateTimer()
87 endif
88 //static members
89 call thistype.m_benchmarks.pushBack(this)
90 set this.m_index = thistype.m_benchmarks.backIndex()
91 return this
92 endmethod
93
94 public method onDestroy takes nothing returns nothing
95 //static members
96 call thistype.m_benchmarks.erase(this.m_index)
97 //members
98 static if (A_JAPI) then
99 if (this.m_stopWatch != -1) then
100 call StopWatchDestroy(this.m_stopWatch)
101 endif
102 else
103 call DestroyTimer(this.m_timer)
104 set this.m_timer = null
105 endif
106 endmethod
107
108 public static method init takes nothing returns nothing
109 //static members
110 set thistype.m_benchmarks = AIntegerVector.create()
111 static if (A_DEBUG_HANDLES) then
112 set thistype.m_units = AUnitVector.create()
113 set thistype.m_items = AItemVector.create()
114 set thistype.m_destructables = ADestructableVector.create()
115 set thistype.m_effects = AEffectVector.create()
116 endif
117 endmethod
118
119 public static method cleanUp takes nothing returns nothing
120 //static members
121 loop
122 exitwhen (thistype.m_benchmarks.empty())
123 call thistype(thistype.m_benchmarks.back()).destroy()
124 endloop
125 call thistype.m_benchmarks.destroy()
126 static if (A_DEBUG_HANDLES) then
127 call thistype.m_units.destroy()
128 call thistype.m_items.destroy()
129 call thistype.m_destructables.destroy()
130 call thistype.m_effects.destroy()
131 endif
132 endmethod
133
134 public static method showBenchmarks takes nothing returns nothing
135 local integer i = 0
136 loop
137 exitwhen (i == thistype.m_benchmarks.size())
138 call thistype(thistype.m_benchmarks[i]).show()
139 set i = i + 1
140 endloop
141 endmethod
142
143 static if (A_DEBUG_HANDLES) then
144 public static method showUnits takes nothing returns nothing
145 local integer i = 0
146 loop
147 exitwhen (i == thistype.m_units.size())
148 debug call Print(GetUnitName(thistype.m_units[i]))
149 set i = i + 1
150 endloop
151 debug call Print("Total count: " + I2S(thistype.m_units.size()) + ".")
152 endmethod
153
154 public static method showItems takes nothing returns nothing
155 local integer i = 0
156 loop
157 exitwhen (i == thistype.m_items.size())
158 debug call Print(GetItemName(thistype.m_items[i]))
159 set i = i + 1
160 endloop
161 debug call Print("Total count: " + I2S(thistype.m_items.size()) + ".")
162 endmethod
163
164 public static method showDestructables takes nothing returns nothing
165 local integer i = 0
166 loop
167 exitwhen (i == thistype.m_destructables.size())
168 debug call Print(GetDestructableName(thistype.m_destructables[i]))
169 set i = i + 1
170 endloop
171 debug call Print("Total count: " + I2S(thistype.m_destructables.size()) + ".")
172 endmethod
173 endif
174
175 public static method showAll takes nothing returns nothing
176 call thistype.showBenchmarks()
177 static if (A_DEBUG_HANDLES) then
178 call thistype.showUnits()
179 call thistype.showItems()
180 call thistype.showDestructables()
181 endif
182 endmethod
183
184 static if (A_DEBUG_HANDLES) then
185 private static method createUnit takes player id, integer unitid, real x, real y, real face returns nothing
186 local unit whichUnit
187 if (thistype.m_units != 0) then //native can be called before ABenchmarks initialization!
188 set whichUnit = CreateUnit(id, unitid, x, y, face)
189 call thistype.m_units.pushBack(whichUnit)
190 call RemoveUnit(whichUnit)
191 set whichUnit = null
192 endif
193 //return thistype.m_units.back()
194 endmethod
195
196 private static method removeUnit takes unit whichUnit returns nothing
197 if (thistype.m_units != 0) then //native can be called before ABenchmarks initialization!
198 call thistype.m_units.remove(whichUnit)
199 endif
200 endmethod
201
202 private static method createItem takes integer itemid, real x, real y returns nothing
203 local item whichItem
204 if (thistype.m_items != 0) then
205 set whichItem = CreateItem(itemid, x, y)
206 call thistype.m_items.pushBack(whichItem)
207 call RemoveItem(whichItem)
208 set whichItem = null
209 endif
210 //return thistype.m_items.back()
211 endmethod
212
213 private static method removeItem takes item whichItem returns nothing
214 if (thistype.m_items != 0) then
215 call thistype.m_items.remove(whichItem)
216 endif
217 endmethod
218
219 private static method createDestructable takes integer objectid, real x, real y, real face, real scale, integer variation returns nothing
220 local destructable whichDestructable
221 if (thistype.m_destructables != 0) then
222 set whichDestructable = CreateDestructable(objectid, x, y, face, scale, variation)
223 call thistype.m_destructables.pushBack(whichDestructable)
224 call RemoveDestructable(whichDestructable)
225 set whichDestructable = null
226 endif
227 //return thistype.m_destructables.back()
228 endmethod
229
230 private static method removeDestructable takes destructable d returns nothing
231 if (thistype.m_destructables != 0) then
232 call thistype.m_destructables.remove(d)
233 endif
234 endmethod
235 endif
236 endstruct
237
238 static if (A_DEBUG_HANDLES) then
239 debug hook CreateUnit ABenchmark.createUnit
240 debug hook RemoveUnit ABenchmark.removeUnit
241 debug hook CreateItem ABenchmark.createItem
242 debug hook RemoveItem ABenchmark.removeItem
243 debug hook CreateDestructable ABenchmark.createDestructable
244 debug hook RemoveDestructable ABenchmark.removeDestructable
245 endif
246
247 endlibrary